package com.sprouting.metadata.core.service.queryImpl;

import com.sprouting.metadata.core.model.SimpleDataSource;
import com.sprouting.metadata.core.utils.StringTool;
import com.sprouting.metadata.core.interfaces.MetadataQueryInterfaces;
import com.sprouting.metadata.core.model.MetadataConfig;
import com.sprouting.metadata.core.utils.JdbcTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;


/**
 *  postgresql 数据库获取信息的实现
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2022/4/20 10:18
 */
public class PGInspector implements MetadataQueryInterfaces {


    /**
     * 获取表名和注释映射
     * @param simpleDataSource 数据源配置
     * @param metadataConfig 配置信息
     */
    @Override
    public Map<String, String> getTable(SimpleDataSource simpleDataSource, MetadataConfig metadataConfig) {
        Map<String, String> answer = new HashMap<String, String>();
        try {
            String sql = "select tname,comments from(select relname as TNAME ,col_description(c.oid, 0) as COMMENTS from pg_class c where  relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%') as temp where comments is not null ";
            Connection connection = simpleDataSource.getConnection();
            // 预编译
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                // 表名
                String key = rs.getString(StringTool.convertLetterByCase("tname", simpleDataSource));
                // 说明
                String value = rs.getString(StringTool.convertLetterByCase("comments", simpleDataSource));
                answer.put(key, value);
            }
            JdbcTool.closeResultSet(rs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return answer;
    }

    /**
     * 获取表名和列名-注释映射
     * @param simpleDataSource 数据源配置
     * @param metadataConfig 配置信息
     */
    @Override
    public Map<String, Map<String, String>> getColumn(SimpleDataSource simpleDataSource, MetadataConfig metadataConfig) {
        Map<String, Map<String, String>> answer = new HashMap<String, Map<String, String>>();
        try {
            StringBuilder sql = new StringBuilder("select tname,cname,comments from( ");
            sql.append("SELECT col_description(a.attrelid,a.attnum) as comments,a.attname as cname,c.relname as tname FROM pg_class as c,pg_attribute as a where a.attrelid = c.oid and a.attnum>0 and c.relname not like 'pg_%' and c.relname not like 'sql_%') as temp where comments is not null ");

            Connection connection = simpleDataSource.getConnection();
            // 预编译
            PreparedStatement preparedStatement = connection.prepareStatement(sql.toString());
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                // 表名
                String tname = rs.getString(StringTool.convertLetterByCase("tname", simpleDataSource));
                if (!answer.containsKey(tname)) {
                    answer.put(tname, new HashMap<>());
                }

                String cname = rs.getString(StringTool.convertLetterByCase("cname", simpleDataSource));
                String value = rs.getString(StringTool.convertLetterByCase("comments", simpleDataSource));
                answer.get(tname).put(cname, value);
            }
            JdbcTool.closeResultSet(rs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return answer;
    }
}

